<?php

namespace app\admin\model;

use think\Model;
use think\Db;
use think\Exception;//事务处理

class Promodel extends Model
{

    function updateData($table,$where,$updateData){
        $res = Db::name($table)->where($where)->update($updateData);
        return $res;
    }

    function deleteData($table,$where){
        $res = Db::name($table)->where($where)->delete();
        return $res;
    }

    function getSearch($table,$alias,$field,$where="1=1",$join,$order){
        $res = Db::name($table)
                    ->alias($alias)
                    ->join($join)
                    ->field($field)
                    ->where($where)
                    ->order($order)
                    // ->fetchSql()
                    ->select();
        return $res;
    }

    function getDatas($table,$field,$where,$order){
        $res = Db::name($table)->field($field)->where($where)->order($order)->select();
        return $res;
    }

    function insertData($table,$insertData){
        $res = Db::name($table)->insertGetId($insertData);
        return $res;
    }


    function getTreeParent($table,$field,$join,$where,$id,$data=array()){
        $_data = Db::name($table)
                    ->alias('zu')
                    ->join($join)
                    ->field($field)
                    ->where($where)
                    ->where('zu.id',$id)
                    ->select();

        if (!empty($_data)) {
            foreach ($_data as $k => $v) {
                // $data[$k] = $_data[$k];
                array_push($data,$_data);
                $res = $this->getTreeParent($table,$field,$join,$where,$v['pid']);
                if (!empty($res)) {
                    // $data[$k]['child'] = $res;
                    array_push($data,$res[$k]);
                }
                // var_dump($data);die;
            }
            krsort($data);
            return $data;
        } else {
            krsort($data);
            return $data;
        }
    }


    function is_delete($table,$where){

        $res = Db::name($table)->where($where)->count();
        return $res;
    }


    function getViewMultiLanguage($switch,$lang='zh'){

        switch ($switch) {
            case '1':
                $res = 'normal';
                // $res = Db::name($table)->where('abbreviation',$table.'_'.$id)->value('lang_'.$lang);
                break;
            case '2':
                $res = 'delete';
                break;
            case '3':
                $res = 'hide';
                break;
            case '4':
                $res = 'Obtained';
                break;
            default:
                $res = $switch;
                break;
        }
        $result = Db::name('lang')->where('abbreviation',$res)->value('lang_'.$lang);
        return $result;
    }

    function delChild($table,$idArray){
        // return $idArray;
        Db::startTrans();
        try{    
            foreach ($idArray as $k => $v) {
                $res = Db::name($table)->where('id',$v)->delete();
            }
            if($res >= 0){
                Db::commit();
                return array('code'=>2000,'msg'=>lang($table.'__operational_success'));
            }else{
                return array('code'=>500,'msg'=>lang($table.'__operational_fail'));
            }
        }catch(\Exception $e){
            Db::rollback();
            return array('code'=>500,'msg'=>$e);
        }
    }


    // 添加子级
    function cateInsert($cateId,$v,$lang){
        // return $v['pclass'];
        if($v['clasname']){
            $drank = (Db::name('product_cate')->where('pid',$v['pclass'])->where('id','neq',$cateId)->order('drank desc')->value('drank'))+1;

            Db::startTrans();
            try{

                $c = $this->updateData('product_cate',['id'=>$cateId],[
                                                                                    'pid'=>$v['pclass'],
                                                                                    'product_cate_status'=>$v['status'],
                                                                                    'abbreviation'=>'product_cate_'.$cateId,
                                                                                    'drank'=>$drank
                                                                                ]);
                $where['abbreviation'] = 'product_cate_'.$cateId;
                $where['model'] = 'product_cate';
                $d = $this->updateData('lang',$where,['lang_'.$lang=>$v['clasname']['value']]);
                if($c >= 0 && $d>=0){
                    Db::commit();
                    return array('code'=>2000,'msg'=>lang('successfully_added'));
                }else{
                    return array('code'=>500,'msg'=>lang('The_route_addition_failed'));
                }
            }catch(\Exception $e){
                Db::rollback();
                return array('code'=>500,'msg'=>$e);
            }
        }else{
            return array('code'=>500,'msg'=>lang('miss data to insert'));
        }
    }




}


